#include "init.h"
#include "sysctrl.h"
#include "uart.h"
#include "gpio.h"

void InitClock()
{
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); 
    Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);
    SysTick_Config(4000);
}



void InitGPIO()
{
    stc_gpio_config_t stcGpioCfg;
    DDL_ZERO_STRUCT(stcGpioCfg);
    stcGpioCfg.enDir = GpioDirOut;
    Gpio_Init(GpioPortA,GpioPin9,&stcGpioCfg);
    Gpio_SetAfMode(GpioPortA,GpioPin9,GpioAf1);//TX
    stcGpioCfg.enDir = GpioDirIn;
    Gpio_Init(GpioPortA,GpioPin10,&stcGpioCfg);
    Gpio_SetAfMode(GpioPortA,GpioPin10,GpioAf1);//RX
}



void InitUart()
{
    uint16_t u16Scnt = 0;
    stc_uart_config_t  stcConfig;
    stc_uart_irq_cb_t stcUartIrqCb;
    stc_uart_multimode_t stcMulti;
    stc_uart_baud_t stcBaud;
    
    en_uart_mmdorck_t enTb8;

    DDL_ZERO_STRUCT(stcConfig);
    DDL_ZERO_STRUCT(stcUartIrqCb);
    DDL_ZERO_STRUCT(stcMulti);
    DDL_ZERO_STRUCT(stcBaud);
    stcUartIrqCb.pfnRxIrqCb   = NULL;
    stcUartIrqCb.pfnTxIrqCb   = NULL;
    stcUartIrqCb.pfnRxFEIrqCb = NULL;
    stcUartIrqCb.pfnPEIrqCb   = NULL;
    stcUartIrqCb.pfnCtsIrqCb  = NULL;
    stcConfig.pstcIrqCb = &stcUartIrqCb;
    stcConfig.bTouchNvic = FALSE;
  	if(TRUE == stcConfig.bTouchNvic)
	{
		EnableNvic(UART0_IRQn,IrqLevel3,TRUE);
	}

    stcConfig.enRunMode = UartMode1;
    stcConfig.enStopBit = Uart1bit;

    stcMulti.enMulti_mode = UartNormal;
	Uart_SetMultiMode(UARTCH1,&stcMulti);
    enTb8 = UartDataOrAddr;
    Uart_SetMMDOrCk(UARTCH0,enTb8); 
    Uart_Init(UARTCH0, &stcConfig);
    
    Uart_SetClkDiv(UARTCH0,Uart8Or16Div);
    stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
    stcBaud.enRunMode = UartMode1;
    stcBaud.u32Baud = 9600;
    u16Scnt = Uart_CalScnt(UARTCH0,&stcBaud);
    Uart_SetBaud(UARTCH0,u16Scnt);
    
    Uart_ClrStatus(UARTCH0,UartRC);
    Uart_EnableFunc(UARTCH0,UartRx);
}









